using TMPro;
using UnityEngine;

public class CollectWeapon : MonoBehaviour
{
    private IPlayer player;
    private Collider2D collision;
    private GameObject NameObj;
    private TextMeshProUGUI text;
    private bool isEnter;
    private void Start()
    {
        Debug.Log($"CollectWeapon Start - GameObject name: {name}");
        if (transform.Find("WeaponName") != null)
        {
            NameObj = transform.Find("WeaponName").gameObject;
            text = NameObj.transform.Find("Text").GetComponent<TextMeshProUGUI>();
            string translation = LanguageCommand.Instance.GetTranslation(name);
            Debug.Log($"CollectWeapon - 翻譯結果: {translation}");
            text.text = translation;
        }
        else
        {
            Debug.LogWarning("CollectWeapon - 找不到 WeaponName 子對象");
        }
    }
    private void Update()
    {
        if (isEnter)
        {
            if (InputUtility.Instance.GetKeyDown(KeyAction.Use))
            {
                if (collision != null)
                {
                    Symbol symbol = collision.GetComponent<Symbol>();
                    if (symbol != null && symbol.GetCharacter() != null)
                    {
                        player = symbol.GetCharacter() as IPlayer;
                        if (player != null)
                        {
                            player.AddWeapon(System.Enum.Parse<PlayerWeaponType>(name));
                            Destroy(gameObject);
                        }
                        else
                        {
                            Debug.LogWarning("無法獲取玩家角色");
                        }
                    }
                    else
                    {
                        Debug.LogWarning("無法獲取Symbol組件或Character");
                    }
                }
                else
                {
                    Debug.LogWarning("碰撞對象為空");
                }
            }
            NameObj?.SetActive(true);
        }
        else
        {
            NameObj?.SetActive(false);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            isEnter = true;
            this.collision = collision;
        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            isEnter = false;
        }
    }
    public void SetWeaponBoxColliderInfo(Vector2 size, Vector2 offect)
    {
        GetComponent<BoxCollider2D>().size = size;
        GetComponent<BoxCollider2D>().offset = offect;
    }
}
